package com.sicheng.datastruct.stack.calculator;

public class Calculator {
    public static void main(String[] args) {


        //算法有问题

        String ex = "3+2*6-2*2*5*2*2-5";
        ArrayStack number = new ArrayStack(20);
        ArrayStack operator = new ArrayStack(20);

        int index = 0;
        while (index < ex.length()) {
            char current = ex.charAt(index);
            if (!number.isOperator(current)) {
                number.push(Integer.parseInt(String.valueOf(current)));
            } else {
                if (operator.isEmpty())
                    operator.push(current);
               else {
                    if (operator.isBiggerThanTop(current)) {
                        operator.push(current);
                    }
                    else {
                        double first = number.pop();
                        double second = number.pop();
                        int op = (int) operator.pop();

                        double v = ArrayStack.calculate(first, second, op);
                        number.push(v);
                        operator.push(current);

                    }

                }
            }
            index++;
        }
        while (!operator.isEmpty()) {
            double first = number.pop();
            double second = number.pop();

            int op = (int) operator.pop();
            double v = ArrayStack.calculate(first, second, op);
            number.push(v);

        }

        number.displayStack();


    }
}
